package Data;

import java.util.TreeSet;

public class Student implements  Comparable<Student> {

  private String name;
  private int age;

  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }

  @Override
  public String toString() {
    return "Data.Student{" +
            "name='" + name + '\'' +
            ", age='" + age + '\'' +
            '}';
  }

  @Override
  public int compareTo(Student o) {
    return this.age-o.age;  //比较年龄（升序）
  }

  public static void main(String[] args) {
    TreeSet<Student> set = new TreeSet<>();

    set.add(new Student("will1",22));
    set.add(new Student("will2",30));
    set.add(new Student("will3",21));

    for (Student student : set) {
      System.out.println(student);
    }
  }
}
